<?php
#  Gfanqiang - Software suite for breakthrough GFW
#
#  by Hong Kong Network Institute of Liberty(NIL), licensed under The GNU General Public License v3.0
#  You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
#  
#  bootstrapgc.php - Bootstrap Phase 2
#  "gc" is stand for googleCode. In the old version, we use GAE for Bootstrap, not GoogleCode.

echo "Bootstrap:\r\n";

/* Set Current Directory */
preg_match('/(.*?)\\\bootstrapgc\.php$/',__FILE__,$currentdir); 
chdir($currentdir[1]);

$hosts=array();
$host = "gfangqiang.googlecode.com";
require_once("makegservers.inc.php");

if(file_exists("custom.ini")){
	$custom = parse_ini_file("custom.ini");
}

$gaeport=is_numeric($custom["gaeport"]) ? $custom["gaeport"] : 8964;
$pacport=is_numeric($custom["pacport"]) ? $custom["pacport"] : 8965;

/* Love */
$love=file_exists("data/love.txt") ? json_encode(file_get_contents("data/love.txt")) : NULL;

/* GOOGLE-IP */
echo " Host file...";
if(file_exists("data/host")){
	echo "Exists!\r\n";
	$ip=file_get_contents("data/host");
	$ip=explode("\n",str_replace("\r\n","\n",$ip));
	foreach($ip as $key=>$value){
		array_unshift($gservers,$value);
	}
	array_unique($gservers);
}else{
	echo "Not Exists!\r\n";
}
echo "\r\n";

/* Get bootstrap.txt and try to get a list of aviliable Gserver IP Address */
echo " Checking GServers Availability:\r\n";
$request="GET /svn/appid.txt HTTP/1.1\r\nHost:{host}\r\nConnection: close\r\n\r\n";

$success=false; $successcount=0; $successtarget=4;
foreach($gservers as $gkey=>$gserver){ 
	if($successcount<$successtarget){
		echo "  Trying ".$gserver."...";
		
		if($fp = @fsockopen('ssl://'.$gserver, 443,$errno,$errstr,3)){
			if (fwrite($fp, str_replace('{host}',$host,$request)) ) {
				while (!feof($fp) ) {
					$response .= fgets($fp, 1024);
				}	
				if(preg_match('/HTTP\/1.1 200 OK/',$response)){
					$response=explode("\r\n\r\n",$response);
					$response=$response[1];
					$success=true;	
					$successcount++;
					$hosts[]=$gservers[$gkey];
					echo "OK! ({$successcount}/{$successtarget})\r\n";
				}else{
					echo "Walled!\r\n";
					unset($gservers[$gkey]);
				}
			}else{
				echo "Walled!\r\n"; // FUCK GFW
				unset($gservers[$gkey]);
			}
			@fclose($fp);
		} elseif(! $fp){
			echo "Walled!\r\n";
			unset($gservers[$gkey]);
			@fclose($fp);
		}
	}
}

echo "\r\n";

/* Load proxy.ini template */
echo " Generating proxy.ini...\r\n";
$template=@file_get_contents("goagent-local/proxy.ini.template");

/* Final Bootstrap: Write proxy.ini */
if(count($hosts)){
	$appid=$response;
	$ini=str_replace("<appid>",$appid,$template);
	$ini=str_replace("<hosts>",implode("|",$hosts),$ini);
	$ini=str_replace("<profile>","google_gfq",$ini);
	$ini=str_replace("<gaeport>",$gaeport,$ini);
	$ini=str_replace("<pacport>",$pacport,$ini);
	$ini=str_replace("<love>",$love,$ini);

	echo "  HOSTS=".implode("|",$hosts)."\r\n";
	$cnhosts=array_filter($hosts,"filter_cn");
	file_put_contents("data/last-known-good",implode("\r\n",$cnhosts));
}else{
	echo "Bootstrap FAILED!\r\n";
	while(true){fgets(STDIN);}
}

/* Output proxy.ini */
if(! file_put_contents("goagent-local/proxy.ini",$ini)){
	echo "ERROR to wrtie proxy.ini!";
}

/* Ping the Counter... PONG!! */
$options = array('http' =>array('timeout'=>2));
$context  = stream_context_create($options);
@file_get_contents("http://14.199.108.78/ping?osver=".urlencode(`ver`),false,$context);

echo "\r\n";
?>